package vip.specimen.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.common.enums.SaveMethodEnum;
import vip.specimen.system.common.implement.AliOssUpload;
import vip.specimen.system.common.implement.QiNiuUpload;
import vip.specimen.system.common.implement.TencentUpload;
import vip.specimen.system.datasource.entity.SystemSaveConfigEntity;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemSaveConfigService;

import javax.annotation.Resource;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/9 11:08
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "文件操作")
@RestController
@RequestMapping(value = "/upload")
public class UploadController {
    @Resource
    private SystemSaveConfigService systemSaveConfigService;

    @PostMapping
    @ApiOperation(value = "上传", notes = "文件上传")
    @RequiresPermissions(value = "file:upload")
    @SystemJurisdiction(resourceType = "button", permissionName = "上传文件")
    public JsonResult upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return JsonResult.failed(ResultCode.ERROR_UPLOAD_NOT_EMPTY);
        }
        SystemSaveConfigEntity config = this.systemSaveConfigService.getConfig();
        String methodName = config.getSaveMethod().name();
        if (methodName.equals(SaveMethodEnum.QiNiu.name())) { // 七牛云存储
            return new QiNiuUpload().upload(file);
        }else if (methodName.equals(SaveMethodEnum.ALiYun.name())){
            return new AliOssUpload().upload(file);
        }else if (methodName.equals(SaveMethodEnum.Tencent.name())){
            return new TencentUpload().upload(file);
        }
        return JsonResult.failed(ResultCode.ERROR_SAVE_NOT_EMPTY);
    }
}
